package com.example.cake.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.cake.common.Result;
import com.example.cake.entity.DTO.SysFilesDTO;
import com.example.cake.entity.SysFiles;
import com.example.cake.service.SysFilesService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("files")
public class SysFilesController {
    // 在application.yml动态获取上传图片的存储路径
    @Value("${files.upload.path}")
    private String basePath;
    //上传
    @Resource
    public SysFilesService sysFilesService;
    @PostMapping("/uploadFile")
    public Result upload(MultipartFile multipartFile) throws IOException {
        String url = sysFilesService.upload(basePath,multipartFile);
        return Result.success(url);
    }
    //下载
    @GetMapping("/{fileUUID}")
    public void download(@PathVariable String fileUUID, HttpServletResponse response) throws IOException{
        if (StringUtils.isNotBlank(fileUUID)){
            sysFilesService.download(basePath,fileUUID,response);
        }
    }
    // 删除
    @PostMapping("/delete")
    public Result deleteCustomer(@RequestBody SysFiles sysFiles) {
        return Result.success(sysFilesService.removeById(sysFiles.getId()));
    }
    //批量删除
    @PostMapping("/batch/delete")
    public Result batchDeleteCustomer(@RequestBody List<Integer> ids) {
        return Result.success(sysFilesService.removeByIds(ids));
    }
    //分页查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody SysFilesDTO filesDTO) {
        return Result.success(sysFilesService.findPageOfFiles(filesDTO));
    }

}
